package com.atlantis.apps.socius.service;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;

import com.atlantis.apps.socius.util.SNConstants;

/**
 */
public class SNBackgroundService extends Service {
	public static final String ACTION_START = "service.start";
	public static final String ACTION_SUBMIT = "service.submit";
	public static final String ACTION_STOP = "service.stop";
	private Timer timer = new Timer();

	// private NotificationManager mNM;

	@Override
	public void onCreate() {
		super.onCreate();
        
		Log.d(getClass().getSimpleName(), "Service Created");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (timer != null)
			timer.cancel();
		Log.d(getClass().getSimpleName(), "Service Destroyed");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		SharedPreferences mySharedPreferences = getSharedPreferences(SNConstants.SNSETTINGS, Activity.MODE_PRIVATE);

		if (mySharedPreferences != null) {
			//TODO - Get the refresh interval form settings
			int refreshInterval = Integer.parseInt("120");

			timer.scheduleAtFixedRate(new TimerTask() {
				public void run() {
					_updateData();
				}
			}, 0, refreshInterval * (1000));
		}

		Log.d(getClass().getSimpleName(), "Service Started");

		return START_STICKY;
	}

	private void _updateData() {
		try {
			//SNUtility.updateData(this);
			Log.d(getClass().getSimpleName(), "Service Update Data");

		} catch (Exception e) {
			Log.d(getBaseContext().getClass().getSimpleName(), "Check your Settings!");
		}
	}


	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}


}
